import { Component, OnInit} from '@angular/core';
import { Router,ActivatedRoute } from '@angular/router';
import { MdDialog,MdTabsModule} from '@angular/material';

import { Doctor } from '../../domain/doctor.mdl';
import { DoctorDialogForm } from './components/doctor.dialog.form';

import { AuthService } from '../../services/auth.service';
import { DoctorService }  from '../../services/doctor.service';

var DlgConfig = {
  disableClose:true,
  hasBackdrop:true,
  width:'680px',
  height:'520px',
};

@Component({
  selector: 'doctor-list',
  templateUrl: 'doctor.component.html',
  styles: [
    `.clkt {
      cursor: pointer;
    }
    .licon{
      width:24px;
      height:24px;
    }
    #locListPanel{
      min-height:360px;
    }
    `
  ]
})
export class DoctorComponent implements OnInit {
  doctors: Doctor[];
  role:string;
  isMonitor:boolean = true;
  selectedDoctor: Doctor;
  constructor(private route: ActivatedRoute,private router: Router,
    private doctorService: DoctorService,
    private authService:AuthService,
    public dialog: MdDialog) {
    //console.log('DoctorComponent constructor calle.');
    //console.log(moment().format('YYYY-MM-DD HH:mm:ss'),'called.');
  }

  getDoctors(type:string): void {
  	layer.msg('type:' + type);
    this.doctorService.getDoctors(type).subscribe(
      doctors => {
        this.doctors = doctors;
      },
      error =>{
        layer.msg('load data failed.'+error);
      }
    );
  }
  
  refreshList(type:string):void {
    this.getDoctors(type);
  }

  ngOnInit(): void {
    //console.log('DoctorComponent:ngOnInit() called.');
    this.refreshList("doctor");
    //console.log('this.isMonitor=',this.isMonitor);
  }
  
  onDoctorDeleted(did: string) {
    this.doctors = this.doctors.filter(function(doctor){
      return doctor._id != did;
    });
  }

  deleteDoctor(did:string):void {
    layerHelper.confirm('Are you sure to delete this Record?', 'Delete Confirm',
      (index)=>{
      this.doctorService.deleteDoctor(did).subscribe(
        delId =>{
          //console.log('delId='+delId);
          this.onDoctorDeleted(delId);
          layer.msg('delete over.');
          //layer4ng.close(index);
        },
        error =>{
          console.log('error='+error);
          layer.msg(error);
        }
      );
    });
  }
  
  openAddDlg():void {
    let dialogRef = this.dialog.open(
      DoctorDialogForm,DlgConfig);

    dialogRef.afterClosed().subscribe(result => {
      console.log(JSON.stringify(result,null,2));
      if(result)
        //this.onHospitalAdded.emit(result);
        this.doctors.unshift(result);
    });
  }

  openEditDlg(formData:any):void {
    let dialogRef = this.dialog.open(DoctorDialogForm,
      $.extend({}, DlgConfig,{data:formData}));

    dialogRef.afterClosed().subscribe(result => {
      //this.selectedOption = result;
      console.log(`Dialog result: ${result}`);
    });
  }
}

